//
//  ForgetPwdViewController.m
//  moon
//
//  Created by beyself on 16/8/3.
//  Copyright © 2016年 beyself. All rights reserved.
//

#import "ForgetPwdViewController.h"
#import "Utils.h"
#import "ResetPwdViewController.h"

@interface ForgetPwdViewController ()

@end

@implementation ForgetPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _mSeconds = 120;
    self.title = @"找回密码";
    
    UIButton* bg = [UIButton buttonWithType:UIButtonTypeCustom];
    bg.frame = CGRectMake(0, 0, SCREEN_WIDTH, 100);
    bg.enabled = NO;
    [bg setImage:[UIImage imageNamed:@"logo"] forState:UIControlStateDisabled];
    [self.view addSubview:bg];
    
    for(int i = 0; i < 2; i++)
    {
        UIView* subview = [[UIView alloc] init];
        subview.frame = CGRectMake(0, 100 + 49*i, SCREEN_WIDTH, 48);
        subview.backgroundColor = WHITE;
        subview.layer.masksToBounds = YES;
        
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, 48, 48);
        button.enabled = NO;
        if(0 == i) [button setImage:[UIImage imageNamed:@"icon_phone"] forState:UIControlStateDisabled];
        else [button setImage:[UIImage imageNamed:@"icon_code"] forState:UIControlStateDisabled];
        [subview addSubview:button];
        
        UITextField* textfield = [[UITextField alloc] init];
        textfield.tag = 1000 + i;
        textfield.frame = CGRectMake(48, 0, SCREEN_WIDTH/2, 48);
        textfield.textColor = BLACK;
        if(i == 0)
        {
            textfield.keyboardType = UIKeyboardTypePhonePad;
            textfield.placeholder = @"您的手机号码";
        }
        else
        {
            textfield.keyboardType = UIKeyboardTypePhonePad;
            textfield.placeholder = @"您收到的验证码";
        }
        [subview addSubview:textfield];
        [self.view addSubview:subview];
        
        if(i == 0)
        {
            _mButton = [UIButton buttonWithType:UIButtonTypeCustom];
            _mButton.frame = CGRectMake(SCREEN_WIDTH - 100, 6, 90, 36);
            _mButton.layer.cornerRadius = 3;
            _mButton.layer.masksToBounds = YES;
            _mButton.titleLabel.font = FONT_12;
            [_mButton setTitleColor:WHITE forState:UIControlStateNormal];
            [_mButton addTarget:self action:@selector(getCode) forControlEvents:UIControlEventTouchUpInside];
            [_mButton setBackgroundImage:[Utils imageFromColor:GREEN] forState:UIControlStateNormal];
            [_mButton setBackgroundImage:[Utils imageFromColor:LINE] forState:UIControlStateHighlighted];
            [_mButton setBackgroundImage:[Utils imageFromColor:GRAY] forState:UIControlStateDisabled];
            [_mButton setTitle:@"发送验证码" forState:UIControlStateNormal];
            [subview addSubview:_mButton];
        }
    }
    
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 210, SCREEN_WIDTH - 20, 44);
    button.layer.cornerRadius = 5;
    button.layer.masksToBounds = YES;
    button.titleLabel.font = FONT_15;
    [button addTarget:self action:@selector(loginBtn) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:[Utils imageFromColor:ORANGE] forState:UIControlStateNormal];
    [button setBackgroundImage:[Utils imageFromColor:LINE] forState:UIControlStateHighlighted];
    [button setTitle:@"下一步" forState:UIControlStateNormal];
    [self.view addSubview:button];
}

- (void) loginBtn
{
    NSString* tel = ((UITextField*)[self.view viewWithTag:1000]).text;
    NSString* pwd = ((UITextField*)[self.view viewWithTag:1001]).text;
    
    if(tel.length == 0 || pwd.length == 0)
    {
        [self showMsg:@"请输入手机号码/验证码"];
        return;
    }
    
    
    NSString* smsCode = [Utils readDefaultUser:@"sms_code"];
    
    if(smsCode == nil || ![pwd isEqualToString:smsCode])
    {
        [self showMsg:@"验证码不正确"];
        return;
    }
    
    NSMutableDictionary* param = [[NSMutableDictionary alloc] init];
    [param setObject:tel forKey:@"phonenumber"];
    [param setObject:pwd forKey:@"code"];
    [Utils sendHttpPost:param method:@"member_active_preview" controller:self flag:0];
}

- (void) getCode
{
    NSString* tel = ((UITextField*)[self.view viewWithTag:1000]).text;
    if(tel.length == 0)
    {
        [self showMsg:@"请输入手机号码"];
        return;
    }
    
    NSMutableDictionary* param = [[NSMutableDictionary alloc] init];
    [param setObject:tel forKey:@"phonenumber"];
    [Utils sendHttpPost:param method:@"get_sms_code" controller:self flag:1];
}

- (void) shortTimer
{
    if(_mSeconds > 0)
    {
        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(shortTimer) userInfo:nil repeats:NO];
        _mSeconds--;
        [_mButton setTitle:[NSString stringWithFormat:@"%d",_mSeconds] forState:UIControlStateDisabled];
    }
    else
    {
        _mSeconds = 120;
        _mButton.enabled = YES;;
    }
}

- (void) httpCallback:(NSDictionary*)data flag:(int)flag
{
    if(flag == 1)
    {
        [Utils saveDefaultUser:data[@"code"] key:@"sms_code"];
        _mButton.enabled = NO;
        [_mButton setTitle:[NSString stringWithFormat:@"%d",_mSeconds] forState:UIControlStateDisabled];
        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(shortTimer) userInfo:nil repeats:NO];
    }
    else
    {
        NSString* tel = ((UITextField*)[self.view viewWithTag:1000]).text;
        
        ResetPwdViewController* subview = [[ResetPwdViewController alloc] init];
        subview.tel = tel;
        [self.navigationController pushViewController:subview animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
